package com.xudow.app.newui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.base.QaExperts;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.newui.PreLoginActivity;
import com.xudow.app.newui.base.BaseUiFragment;
import com.xudow.app.ui.ExpertDetailActivity;
import com.xudow.app.ui.adapter.ExportAdapter;
import com.xudow.app.ui.support.RecyclerViewScrollLocationListener;
import com.xudow.app.ui.support.XLinearLayoutManager;
import com.xudow.app.ui.task.ExportTask;
import com.xudow.app.ui.task.qanda.QuestionSubmitTask;
import com.xudow.app.util.Maps;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 提问fragment
 */
public class AskFragment extends BaseUiFragment implements ExportAdapter.OnRecyclerViewListener, View.OnClickListener {
    @BindView(R.id.question)
    TextView questionView;

    @BindView(R.id.commit)
    Button commit;

    @BindView(R.id.exports)
    RecyclerView exportsRecyclerView;

    private ExportAdapter exportAdapter;
    private List<QaExperts> exports;

    private ExportTask exportTask;
    private QuestionSubmitTask questionSubmitTask;

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,Bundle savedInstanceState) {



        return inflater.inflate(R.layout.fragment_ask, container, false);
    }

    /**
     * 获取专家
     */
    private void loadExperts() {
        exportTask = new ExportTask(getMyAct(), exportHandler);
        getMyAct().addTask(exportTask);
        exportTask.execute();
    }
    /**
     * 专家Handler
     */
    private Handler exportHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (ExportTask.SUCCESS == msg.what) {
                Bundle data = msg.getData();
                QaExperts[] qaExpertses = (QaExperts[]) data.getSerializable("exports");

                if (null == qaExpertses || qaExpertses.length <= 0) {
                    return;
                }

                for (QaExperts expert : qaExpertses) {
                    exports.add(expert);
                }

                exportAdapter.notifyDataSetChanged();
            }
        }
    };

    /**
     * 问题提交按钮事件
     *
     */
    public void onSubmitClick() {
        String question = questionView.getText().toString().trim();

        XApplication application = getApp();

        if (!application.isLogin()) {
            questionView.setError(getString(R.string.qanda_login_tip));
            startActivity(new Intent(getContext(), PreLoginActivity.class));
            return;
        }

        if (TextUtils.isEmpty(question)) {
            questionView.setError(getString(R.string.question_required));
            return;
        }

        HashMap<String, String> params = Maps.newHashMap();
        params.put("question", question);

        showLodingDialog(getString(R.string.submitting));

        questionSubmitTask = new QuestionSubmitTask(getMyAct(), questionSubmitTaskHandler);
        questionSubmitTask.execute(params);
    }

    /**
     * 新增提问Handler
     */
    private Handler questionSubmitTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            hideLoadingDialog();
            String message="";
            if (QuestionSubmitTask.SUCCESS == msg.what) {
                questionView.setText("");

                 message = getString(R.string.submit_success);
            } else {
                 message = getString(R.string.submit_fail);
            }
            getApp().makeToast(message);
        }
    };

    int page=0;
    @Override
    protected void initData() {
        exportsRecyclerView.setHasFixedSize(true);

        XLinearLayoutManager layoutManager = new XLinearLayoutManager(getMyAct());
        layoutManager.setOnRecyclerViewScrollLocationListener(exportsRecyclerView,
                new RecyclerViewScrollLocationListener() {
                    @Override
                    public void onTopWhenScrollIdle(RecyclerView recyclerView) {
                    }

                    @Override
                    public void onBottomWhenScrollIdle(RecyclerView recyclerView) {
                        // TODO 下拉加载跟多
                        page++;
//                        getApp().makeToast("下拉加载跟多"+page);
                    }
                });
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        exportsRecyclerView.setLayoutManager(layoutManager);

        exports = new ArrayList<QaExperts>();

        exportAdapter = new ExportAdapter(getMyAct(), exports);
        exportsRecyclerView.setAdapter(exportAdapter);
        exportAdapter.setOnRecyclerViewListener(this);


        loadExperts();
    }

    @Override
    protected void initListener() {
        commit.setOnClickListener(this);
    }

    @Override
    public void onItemClick(int position) {//专家详情
        QaExperts expert = exports.get(position);

        Intent intent = new Intent(getMyAct(), ExpertDetailActivity.class);
        intent.putExtra("expertId", expert.getId());
        startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(int position) {
        return false;
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.commit)
            onSubmitClick();
    }
}
